﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.HYSpace.SpaceManager.Manager;
using YArchitech.LIB;

namespace YArchitech.HYSpace.SpaceManager.Forms
{
	public partial class AddSpaceManagerFrom : System.Windows.Forms.Form
	{
		public bool IsModify
		{
			get
			{
				return this.m_IsModify;
			}
			set
			{
				this.m_IsModify = value;
			}
		}

		public DataManager DataManager
		{
			get
			{
				return this.m_dataManager;
			}
			set
			{
				this.m_dataManager = value;
			}
		}

		public Zone Zone
		{
			get
			{
				return this.m_zone;
			}
			set
			{
				this.m_zone = value;
			}
		}

		public AddSpaceManagerFrom()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
		}

		public AddSpaceManagerFrom(DataManager dataManager)
		{
			this.InitializeComponent();
			this.m_dataManager = dataManager;
		}

		private void InitUI()
		{
			this.comboBoxLevel.DataSource = this.m_dataManager.Levels;
			this.comboBoxLevel.DisplayMember = "Name";
			this.comboBoxLevel.DropDownStyle = ComboBoxStyle.DropDownList;
			SpaceSet spaceSet;
			if (this.m_zone != null)
			{
				spaceSet = this.m_zone.Spaces;
				Level level = this.m_dataManager.GetLevel(this.m_zone.LevelId);
				if (level != null)
				{
					this.comboBoxLevel.Text = level.Name;
				}
				this.comboBoxLevel.Enabled = false;
			}
			else
			{
				spaceSet = this.m_dataManager.GetSpacesInDefaultZone();
			}
			foreach (Level level2 in this.m_dataManager.GetLevels())
			{
				this.m_dataManager.Update(level2);
				int index = this.spaceTreeView.Nodes.Add(new LevelNode(level2));
				foreach (Space space in this.m_dataManager.GetSpaces())
				{
					int index2 = this.spaceTreeView.Nodes[index].Nodes.Add(new SpaceNodeEx(space));
					if (spaceSet.Contains(space))
					{
						this.spaceTreeView.Nodes[index].Nodes[index2].Checked = true;
					}
				}
			}
			this.spaceTreeView.Sort();
		}

		private SpaceSet GetSelectSpace()
		{
			SpaceSet spaceSet = new SpaceSet();
			foreach (object obj in this.spaceTreeView.Nodes)
			{
				foreach (object obj2 in ((LevelNode)obj).Nodes)
				{
					SpaceNodeEx spaceNodeEx = (SpaceNodeEx)obj2;
					if (spaceNodeEx.Checked)
					{
						spaceSet.Insert(spaceNodeEx.Space);
					}
				}
			}
			foreach (object obj3 in this.m_zone.Spaces)
			{
				Space space = obj3 as Space;
				if (spaceSet.Contains(space))
				{
					spaceSet.Erase(space);
				}
			}
			return spaceSet;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			bool flag = this.m_dataManager.ZoneNameExist(this.textZoneName.Text);
			flag = (this.m_IsModify ? (!this.m_strOldName.Equals(this.textZoneName.Text) && flag) : flag);
			DialogResult dialogResult = DialogResult.OK;
			if (flag)
			{
				dialogResult = YJKMessageBox.Warning("分区名称重复，是否忽略？");
			}
			if (dialogResult == DialogResult.OK)
			{
				if (this.m_IsModify)
				{
					this.m_zone.Name = this.textZoneName.Text;
					this.m_dataManager.UpdataZoneSpaces(this.m_spaceSet);
				}
				else
				{
					Level level = this.comboBoxLevel.SelectedItem as Level;
					if (level == null)
					{
						YJKMessageBox.Error("请选择分区所在的楼层平面！");
						return;
					}
					this.m_dataManager.Update(level);
					this.m_zone = this.m_dataManager.CreateZoneEx(this.textZoneName.Text);
					if (this.m_zone == null)
					{
						this.m_bHoldForm = true;
						return;
					}
					this.m_dataManager.UpdataZoneSpaces(this.m_spaceSet);
				}
				this.m_bHoldForm = false;
				return;
			}
			this.textZoneName.Focus();
			this.m_bHoldForm = true;
		}

		private void spaceTreeView_AfterCheck(object sender, TreeViewEventArgs e)
		{
			if (e.Node.GetType().Name == typeof(SpaceNodeEx).Name)
			{
				SpaceNodeEx spaceNodeEx = e.Node as SpaceNodeEx;
				if (spaceNodeEx == null)
				{
					return;
				}
				if (spaceNodeEx.Checked)
				{
					this.m_spaceSet.Insert(spaceNodeEx.Space);
					return;
				}
				this.m_spaceSet.Erase(spaceNodeEx.Space);
				return;
			}
			else
			{
				LevelNode levelNode = e.Node as LevelNode;
				if (levelNode == null)
				{
					return;
				}
				foreach (object obj in levelNode.Nodes)
				{
					SpaceNodeEx spaceNodeEx2 = obj as SpaceNodeEx;
					if (spaceNodeEx2 != null)
					{
						if (levelNode.Checked)
						{
							this.m_spaceSet.Insert(spaceNodeEx2.Space);
						}
						else
						{
							this.m_spaceSet.Erase(spaceNodeEx2.Space);
						}
					}
				}
				return;
			}
		}

		private void AddSpaceManagerFrom_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_bHoldForm)
			{
				e.Cancel = true;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.m_bHoldForm = false;
		}

		private void AddSpaceManagerFrom_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			if (this.m_IsModify)
			{
				this.textZoneName.Text = this.m_zone.Name;
				this.Text = "修改分区[" + this.m_zone.Name + "]";
				this.m_strOldName = this.m_zone.Name;
			}
			else
			{
				this.textZoneName.Text = "新建分区";
				this.Text = "新建分区";
			}
			this.InitUI();
		}

		private Zone m_zone;

		private DataManager m_dataManager;

		private string m_strOldName = "";

		private bool m_IsModify;

		private SpaceSet m_spaceSet = new SpaceSet();

		private bool m_bHoldForm;
	}
}
